import ConfirmBoxVue from './ConfirmBoxVue.vue'
import Vue from 'vue'
// 参考地址
// https://www.jianshu.com/p/050b9d10e829

var ConfirmBoxConstructor = Vue.extend(ConfirmBoxVue)

let instance, boxObj
const defaultCallback = action => {
  if (action) {
    boxObj.resolve()
  } else {
    boxObj.reject()
  }
}

var show = function(options, callback) {
  if (!instance) {
    instance = new ConfirmBoxConstructor({
      el: document.createElement('div')
    })
    if (!instance.callback) {
      instance.callback = defaultCallback
    }
  }
  // 为了每次弹框的时候内容都能修改
  for (var prop in options) {
    // if (options.hasOwnProperty(prop)) {
    instance[prop] = options[prop]
    // }
  }
  document.body.appendChild(instance.$el)

  if (typeof Promise !== 'undefined') {
    return new Promise(function(resolve, reject) {
      // eslint-disable-line
      boxObj = {
        callback: callback,
        resolve: resolve,
        reject: reject
      }
      // Vue.nextTick(() => {
      instance.mask.show()
      // })
    })
  } else {
  }
}

var hide = function() {
  if (instance) {
    Vue.nextTick(() => {
      instance.mask.hide()
    })
  }
}

var ConfirmBox = {
  show,
  hide
}

export default ConfirmBox
// export { ConfirmBox, hideBox }
